import pageination from '../../components/pageination/pageination.vue'
export default {
	data() {
		return {
			//数据
      total: 0, //总条数
      page: 1, //当前页
      size: 10, //每页显示条数
      search: '', //搜索信息
      modelShow: false,
			ProjectModelShow: false,
			demandModelShow: false,
			WorkModelShow: false,
      projectDetailsModelShow: false,
      validate: {}, //验证对象
      expertInfo: {}, //专家信息
      template: {}, //模板对象
      project: {}, //项目对象
      projectApplication: {}, //项目申请信息
      projectList: [], //项目列表
			uploadNum: 0,
			FileUpload: [],
			curId: 0,
		}
	},
	components: {
		pageination
	},
	created() { //调用方法
		let that = this;
		//查询当前专家信息
    that.$post(
      that.urls.getCurrentExpert
    )
    .then(function(res) {
      if(res.success) {
        that.expertInfo = res.data;
      }
    })
    this.getTemplate();
	},
	mounted() {
    let that = this;
	  that.validate = $("#projectApplicationForm").validate({
      ignore: [],
      rules: {
      /*  expertId: { // 专家id
          required: true
        },
        projectId: { // 项目id
          required: true
        },*/
        applicationBookUrl: { // 申请书
          required: true,
          maxlength: 500
        }
      },
      messages: {
      /*  expertId: { // 专家id
          required: "专家信息获取失败！"
        },
        projectId: { // 课程简介
          required: "项目信息获取失败！"
        },*/
        applicationBookUrl: { // 课程课件
          required: "请上传附件",
          maxlength: "申请书地址大于500"
        }
      },
      /*errorElement: "em",
      errorPlacement: function(error, element) {
        error.appendTo(element.parents(".error-box").children(".error-msg"));
      }*/
    });

  },
	methods: { //写函数方法
		//页码切换执行方法
		pageFn: function(val, pid) {
			this.page = parseInt(val);
			//此处调列表数据接口
      //初始化分页信息
      this.searchTodo();
		},
		/**返回子组件用户选择的一页显示条数**/
		obtainChild: function(data) {
			this.row = data;
		},
    //显示项目详情
    showProjectDetailsModel: function(id) {
		  this.getProject(id);
		  this.projectDetailsModelShow = true;
    },
		touchOffIdInput(n) {
			this.uploadNum = n;
			if(n == 1) {
				$("#handIdFront").click();
			}
		},
		/**文件上传js**/
		idFrontImg: function(event) {
      let that = this;
      var file = event.currentTarget.files[0];
      if (!that.fileGlobalFilter(file)) {
        return;
      }
      let formData = new FormData();
      formData.append('fileNames', file);
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
      .then(function(res) {
        if(res.success) {
          let fileInfos = res.data;
          let fileInfo = fileInfos[0];
          that.FileUpload = [that.getFileName(fileInfo.url)];
          that.projectApplication.applicationBookUrl = fileInfo.url;
          $("#applicationBookUrl-error").css("display","none");
        }
      })
		},
		//显示项目申请
    showProjectApplication: function(projectId) {
      //清空表单校验提示
		  $("#projectApplicationForm").validate().resetForm();
		  //清空表单数据
      $("input[type='reset']").trigger("click");
      //初始化数据
      this.projectApplication.expertId = this.expertInfo.id;
      this.projectApplication.projectId = projectId;
      this.projectApplication.applicationBookUrl = "";
      this.FileUpload = [];
      //显示表单数据
      this.ProjectModelShow = true;
    },
		//专家申请模态框
		demand: function() {
			this.demandModelShow = true;
		},
		//评分标准
		rorkRecord: function() {
			this.WorkModelShow = true;
		},
		//关闭模态框
		closeModel: function() {
			this.ProjectModelShow = false;
			this.demandModelShow = false;
      this.projectDetailsModelShow = false;
		},
		closeModels: function() {
			this.WorkModelShow = false;
		},
    //预览
    preview: function(url,msg) {
      if (url == null || url == "") {
        if (msg == null || msg == "") {
          msg = "附件";
        }
        layer.msg("暂无" + msg + "！");
      } else {
        window.open(this.urls.baseUrl + url);
      }
    },
    //删除附件
    deleteEnclosure: function() {
		  this.FileUpload = [];
	      this.projectApplication.applicationBookUrl = "";
	      $("input[name='applicationBookUrl']").val("");
	      $("#applicationBookUrl-error").css("display","block");
    },
    //审核状态applicationStatus（0：待评审；1：评审中；2：待审核；3：审核通过；4：审核不通过）
    //申请状态applicationStatus（0：未申请；1：已申请）
    auditStatus: function(applicationStatus,applicationAuditStatus) {
      if (applicationStatus == 0) {
        return "待申请";
      } else if (applicationStatus == 1) { //已申请
        if (applicationAuditStatus != null) {
          switch (applicationAuditStatus) {
            case 0:
            case 1:
            case 2:
              return "待审核";
            case 3:
              return "通过";
            case 4:
              return "不通过";
          }
        }
      }
    },
    //获取项目
    getProject: function(id) {
      let that = this;
      that.project = {};
      if (id != null) {
        that.$post(
          that.urls.getProjectInfo, {
            "id": id
          }
        )
          .then(function(res) {
            if(res.success) {
              that.project = res.data;
            }
          })
      }
    },
    /*项目申请信息*/
    searchTodo: function () {
      let that = this;
      that.$post(
        that.urls.getCurrentExpertProjectApplicationPageInfo,
        {"pageNum": that.page,"pageSize": that.size,"search": that.search}
      )
      .then(function(res) {
        if(res.success) {
          that.projectList = res.pageInfo.list;
          that.total = res.pageInfo.total;
          that.page = res.pageInfo.pageNum;
        }
      })
    },
    //申请项目
    applicationProject: function () {
      let that = this;
      let flg = that.validate.form();
      if (flg) {
        that.$post(
          that.urls.addProjectApplication,
          that.projectApplication
        )
        .then(function(res) {
          let msg = "";
          if(res.success) {
            msg = "申请成功！";
            //从新渲染列表数据
            that.searchTodo();
            //关闭窗口
            that.ProjectModelShow = false;
          } else {
            msg = "申请失败！";
          }
          layer.msg(msg, {
            time: 3000, //3s后自动关闭
          });
        })
      }
    },
    //获取模板信息
    getTemplate: function() {
      let that = this;
      //模板类型(0：其他;1:基地申请；2：项目申报；3：项目申请；4：合作合同；5：任务书；6：中期报告；7：结题报告)
      that.$post(
        that.urls.getTemplate, {
          "type": 3
        }
      )
        .then(function(res) {
          if(res.success) {
            that.template = res.data;
          }
        })
    },
    //项目需求书模板下载
    download: function () {
      let url = this.template.url;
      if (url == null || url == "") {
        layer.msg("请联系管理员上传【项目申请书】", {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      window.location.href = this.urls.download + "?path=" + url;
    }
	}
}
